package com.alisls.activiti.service.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.annotation.Resource;

/**
 * SpringSecurity配置类
 *
 * @author Ke Wang
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource(name = "customUserDetailService")
    private UserDetailsService userDetailsService;

    @Resource
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Resource
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Resource
    private LogoutSuccessHandler logoutSuccessHandler;

    @Resource
    private AuthenticationEntryPoint authenticationEntryPoint;

    /**
     * 定义密码编码器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     *
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        // 是否启用调试
        //web.debug(true);
    }

    /**
     *
     * @param auth
     * @throws Exception
     */
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 设置查询用户详情服务
        auth.userDetailsService(userDetailsService);
    }

    /**
     * 安全拦截机制
     * @param http http认证
     * @throws Exception 异常
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                // 默认登录处理地址 /login，默认放行
                .loginProcessingUrl("/user/login")
                // 登录成功处理器
                .successHandler(authenticationSuccessHandler)
                // 登录失败处理器
                .failureHandler(authenticationFailureHandler)
            .and()
                .logout()
                // 默认退出处理地址
                .logoutUrl("/user/logout")
                // 退出成功处理器
                .logoutSuccessHandler(logoutSuccessHandler)
            .and()
                .authorizeRequests()
                // 所有请求需要认证后才可以访问
                .anyRequest().authenticated()
            .and()
                // 关闭跨站请求伪造
                .csrf().disable()
            .exceptionHandling()
                // 处理默认的302状态码
                .authenticationEntryPoint(authenticationEntryPoint);
    }
}

